package com.adwin.addcoin.ui.main

import android.app.Application
import android.provider.Telephony.Mms.Addr
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.adwin.addcoin.AddCoinApplication
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.bean.CheckInBean
import com.adwin.addcoin.repository.bean.HomeTaskBean
import com.adwin.addcoin.repository.net.RetrofitClient
import com.adwin.addcoin.utils.PersonalMessageUtils
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.json.JSONObject
import java.util.concurrent.atomic.AtomicBoolean


class TaskFragmentModel(application: Application) : AndroidViewModel(application) {
    var date = MutableLiveData<CheckInBean>()
    var inviteNumber = MutableLiveData(0)
    var inviteCoins = MutableLiveData(0)
    var pageSize = 15
    var taskLists = MutableLiveData<ArrayList<HomeTaskBean>>()
    var isLoad = MutableLiveData(false)
    var isCheckIn = MutableLiveData<Boolean>()
    var isRefresh = MutableLiveData(false)

    fun getCheckMessage() {
         RetrofitClient.getInstance().getService(TestApi::class.java).getCheckInMessage().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                it.body()?.let { checkBean ->
                    if (checkBean.code == 200){
                        it.body()?.data?.let {bean ->
                            Log.d("checkIn","${Gson().toJson(bean)}")
                            date.value = bean
                        }
                    }
                }
            }else{

            }
        },{
            Log.d("checkIn","${it.message?:"throwable message is null"}")

        })

    }

    /**
     * 查询邀请用户奖励
     */
    fun getInviteRewards(){
        RetrofitClient.getInstance().getService(TestApi::class.java).getInviteRewards().compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful){
                it.body()?.let {baseResponse ->
                    if (baseResponse.code == 200){
                        baseResponse.data?.invitRewardsMap?.keys?.iterator()?.forEach {
                            if (it == "1"){
                                val number = baseResponse.data?.invitRewardsMap[it]?:0
                                inviteNumber.value = number
//                                PersonalMessageUtils.inviteNumber = number
                            }else{
                                val number = baseResponse.data?.invitRewardsMap[it]?:0
                                inviteCoins.value = number
//                                PersonalMessageUtils.inviteCoins = number
                            }
                        }
                    }
                }
            }
        },{

        })
    }

    fun getCoinActivityList(pageNum:Int){
        isRefresh.value = false
        val json = JSONObject()
        json.put("pageNum",pageNum)
        json.put("pageSize",pageSize)
        val requestBody: RequestBody = MultipartBody.create(MediaType.get("application/json;charset=utf-8"), json.toString())
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .getCoinActivityList(requestBody)
            .compose(
            SchedulerTransformer()
        ).subscribe({
                    if (it.isSuccessful){
                        it.body()?.let { data ->
                            if (data.code == 200){
                                if (pageNum == 1){
                                    taskLists.value?.clear()
                                }
                                val lists = ArrayList<HomeTaskBean>()
                                taskLists.value?.let {taskBeans ->
                                    isLoad.value = taskBeans.size >= pageSize
                                    lists.addAll(taskBeans)
                                }
                                if (data.data!=null){
                                    lists.addAll(data.data.list)
                                    taskLists.value = lists
                                }
                            }
                        }
                    }
                isRefresh.value = true
        },{
                isRefresh.value = true
                Log.d("error","${it.message?:"throwable message is null"}")
        })
    }


    fun checkIn(){
        RetrofitClient.getInstance().getService(TestApi::class.java).checkIn().compose(
            SchedulerTransformer()
        ).subscribe({
            val activity = AddCoinApplication.instance.activityDd
            if (it.isSuccessful){
                it.body()?.data?.let {
                    isCheckIn.value = it.status

                }
                if (activity != null){
                    Toast.makeText(activity,it.body()?.msg?:"",Toast.LENGTH_SHORT).show()
                }
            }else{
                if (activity != null){
                    Toast.makeText(activity,it.message()?:"",Toast.LENGTH_SHORT).show()
                }
            }
        },{
            it.message?.let {message ->
                val activity = AddCoinApplication.instance.activityDd
                if (activity != null){
                    Toast.makeText(activity,message,Toast.LENGTH_SHORT).show()
                }
            }
        })

    }
}